import { get, post, put, del } from '@/utils/request';

/**
 * 内容管理API服务
 */

// ==================== 轮播图管理 ====================

/**
 * 获取轮播图列表
 */
export async function getBannerList(params: CONTENT.BannerListParams): Promise<CONTENT.ApiResponse<{ list: CONTENT.BannerModel[]; pagination: CONTENT.PaginationInfo }>> {
  return get('/admin/banners', params);
}

/**
 * 获取轮播图详情
 */
export async function getBannerDetail(id: number): Promise<CONTENT.ApiResponse<CONTENT.BannerModel>> {
  return get(`/admin/banners/${id}`);
}

/**
 * 创建轮播图
 */
export async function createBanner(data: CONTENT.CreateBannerParams): Promise<CONTENT.ApiResponse<CONTENT.BannerModel>> {
  return post('/admin/banners', data);
}

/**
 * 更新轮播图
 */
export async function updateBanner(id: number, data: CONTENT.UpdateBannerParams): Promise<CONTENT.ApiResponse<CONTENT.BannerModel>> {
  return put(`/admin/banners/${id}`, data);
}

/**
 * 删除轮播图
 */
export async function deleteBanner(id: number): Promise<CONTENT.ApiResponse<null>> {
  return del(`/admin/banners/${id}`);
}

/**
 * 批量删除轮播图
 */
export async function batchDeleteBanners(ids: number[]): Promise<CONTENT.ApiResponse<null>> {
  return post('/admin/banners/batch-delete', { banner_ids: ids });
}

/**
 * 更新轮播图排序
 */
export async function updateBannerSort(data: Array<{ id: number; sort_order: number }>): Promise<CONTENT.ApiResponse<null>> {
  return put('/admin/banners/sort', { banners: data });
}

/**
 * 更新轮播图状态
 */
export async function updateBannerStatus(id: number, status: 'active' | 'inactive'): Promise<CONTENT.ApiResponse<null>> {
  return put(`/admin/banners/${id}/status`, { status });
}

/**
 * 复制轮播图
 */
export async function copyBanner(id: number): Promise<CONTENT.ApiResponse<CONTENT.BannerModel>> {
  return post(`/admin/banners/${id}/copy`);
}

// ==================== 活动管理 ====================

/**
 * 获取活动列表
 */
export async function getActivityList(params: CONTENT.ActivityListParams): Promise<CONTENT.ApiResponse<{ list: CONTENT.ActivityModel[]; pagination: CONTENT.PaginationInfo }>> {
  return request('/admin/activities', {
    method: 'GET',
    params,
  });
}

/**
 * 获取活动详情
 */
export async function getActivityDetail(id: number): Promise<CONTENT.ApiResponse<CONTENT.ActivityModel>> {
  return request(`/admin/activities/${id}`, {
    method: 'GET',
  });
}

/**
 * 创建活动
 */
export async function createActivity(data: CONTENT.CreateActivityParams): Promise<CONTENT.ApiResponse<CONTENT.ActivityModel>> {
  return request('/admin/activities', {
    method: 'POST',
    data,
  });
}

/**
 * 更新活动
 */
export async function updateActivity(id: number, data: CONTENT.UpdateActivityParams): Promise<CONTENT.ApiResponse<CONTENT.ActivityModel>> {
  return request(`/admin/activities/${id}`, {
    method: 'PUT',
    data,
  });
}

/**
 * 删除活动
 */
export async function deleteActivity(id: number): Promise<CONTENT.ApiResponse<null>> {
  return request(`/admin/activities/${id}`, {
    method: 'DELETE',
  });
}

/**
 * 更新活动状态
 */
export async function updateActivityStatus(id: number, status: 'active' | 'inactive'): Promise<CONTENT.ApiResponse<null>> {
  return request(`/admin/activities/${id}/status`, {
    method: 'PUT',
    data: { status },
  });
}

/**
 * 复制活动
 */
export async function copyActivity(id: number): Promise<CONTENT.ApiResponse<CONTENT.ActivityModel>> {
  return request(`/admin/activities/${id}/copy`, {
    method: 'POST',
  });
}

/**
 * 导出活动数据
 */
export async function exportActivities(params: CONTENT.ActivityListParams & { format: 'excel' | 'csv' }): Promise<Blob> {
  return request('/admin/activities/export', {
    method: 'GET',
    params,
    responseType: 'blob',
  });
}

/**
 * 获取活动参与者列表
 */
export async function getActivityParticipants(id: number, params?: { page?: number; limit?: number }): Promise<CONTENT.ApiResponse<any>> {
  return request(`/admin/activities/${id}/participants`, {
    method: 'GET',
    params,
  });
}

/**
 * 导出活动参与者
 */
export async function exportActivityParticipants(id: number, format: 'excel' | 'csv'): Promise<Blob> {
  return request(`/admin/activities/${id}/participants/export`, {
    method: 'GET',
    params: { format },
    responseType: 'blob',
  });
}

// ==================== 推荐内容管理 ====================

/**
 * 获取推荐内容列表
 */
export async function getRecommendationList(params: CONTENT.RecommendationListParams): Promise<CONTENT.ApiResponse<{ list: CONTENT.RecommendationModel[]; pagination: CONTENT.PaginationInfo }>> {
  return request('/admin/recommendations', {
    method: 'GET',
    params,
  });
}

/**
 * 获取推荐内容详情
 */
export async function getRecommendationDetail(id: number): Promise<CONTENT.ApiResponse<CONTENT.RecommendationModel>> {
  return request(`/admin/recommendations/${id}`, {
    method: 'GET',
  });
}

/**
 * 创建推荐内容
 */
export async function createRecommendation(data: CONTENT.CreateRecommendationParams): Promise<CONTENT.ApiResponse<CONTENT.RecommendationModel>> {
  return request('/admin/recommendations', {
    method: 'POST',
    data,
  });
}

/**
 * 更新推荐内容
 */
export async function updateRecommendation(id: number, data: CONTENT.UpdateRecommendationParams): Promise<CONTENT.ApiResponse<CONTENT.RecommendationModel>> {
  return request(`/admin/recommendations/${id}`, {
    method: 'PUT',
    data,
  });
}

/**
 * 删除推荐内容
 */
export async function deleteRecommendation(id: number): Promise<CONTENT.ApiResponse<null>> {
  return request(`/admin/recommendations/${id}`, {
    method: 'DELETE',
  });
}

/**
 * 更新推荐内容排序
 */
export async function updateRecommendationSort(data: Array<{ id: number; sort_order: number }>): Promise<CONTENT.ApiResponse<null>> {
  return request('/admin/recommendations/sort', {
    method: 'PUT',
    data: { recommendations: data },
  });
}

// ==================== 功能模块管理 ====================

/**
 * 获取功能模块列表
 */
export async function getFunctionModuleList(params: CONTENT.FunctionModuleListParams): Promise<CONTENT.ApiResponse<{ list: CONTENT.FunctionModuleModel[]; pagination: CONTENT.PaginationInfo }>> {
  return request('/admin/function-modules', {
    method: 'GET',
    params,
  });
}

/**
 * 获取功能模块详情
 */
export async function getFunctionModuleDetail(id: number): Promise<CONTENT.ApiResponse<CONTENT.FunctionModuleModel>> {
  return request(`/admin/function-modules/${id}`, {
    method: 'GET',
  });
}

/**
 * 创建功能模块
 */
export async function createFunctionModule(data: CONTENT.CreateFunctionModuleParams): Promise<CONTENT.ApiResponse<CONTENT.FunctionModuleModel>> {
  return request('/admin/function-modules', {
    method: 'POST',
    data,
  });
}

/**
 * 更新功能模块
 */
export async function updateFunctionModule(id: number, data: CONTENT.UpdateFunctionModuleParams): Promise<CONTENT.ApiResponse<CONTENT.FunctionModuleModel>> {
  return request(`/admin/function-modules/${id}`, {
    method: 'PUT',
    data,
  });
}

/**
 * 删除功能模块
 */
export async function deleteFunctionModule(id: number): Promise<CONTENT.ApiResponse<null>> {
  return request(`/admin/function-modules/${id}`, {
    method: 'DELETE',
  });
}

/**
 * 更新功能模块排序
 */
export async function updateFunctionModuleSort(data: Array<{ id: number; sort_order: number }>): Promise<CONTENT.ApiResponse<null>> {
  return request('/admin/function-modules/sort', {
    method: 'PUT',
    data: { modules: data },
  });
}

// ==================== 内容统计 ====================

/**
 * 获取内容统计数据
 */
export async function getContentStatistics(): Promise<CONTENT.ApiResponse<CONTENT.ContentStatistics>> {
  return request('/admin/statistics/content', {
    method: 'GET',
  });
}

// ==================== 通用功能 ====================

/**
 * 获取内容类型选项
 */
export async function getContentTypeOptions(): Promise<CONTENT.ApiResponse<Array<{ label: string; value: string }>>> {
  return request('/admin/content/type-options', {
    method: 'GET',
  });
}

/**
 * 获取位置选项
 */
export async function getPositionOptions(): Promise<CONTENT.ApiResponse<Array<{ label: string; value: string }>>> {
  return request('/admin/content/position-options', {
    method: 'GET',
  });
}

/**
 * 获取链接类型选项
 */
export async function getLinkTypeOptions(): Promise<CONTENT.ApiResponse<Array<{ label: string; value: string }>>> {
  return request('/admin/content/link-type-options', {
    method: 'GET',
  });
}

/**
 * 搜索内容（用于推荐内容选择）
 */
export async function searchContent(params: { keyword?: string; type?: string; limit?: number }): Promise<CONTENT.ApiResponse<Array<{ id: number; title: string; type: string }>>> {
  return request('/admin/content/search', {
    method: 'GET',
    params,
  });
}

/**
 * 预览内容
 */
export async function previewContent(type: string, id: number): Promise<CONTENT.ApiResponse<any>> {
  return request('/admin/content/preview', {
    method: 'GET',
    params: { type, id },
  });
}
